package com.shenma2009.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenma2009.mapper.TbSecKillMapper;
import com.shenma2009.pojo.TbSecKill;
import com.shenma2009.service.TbSecKillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: KillTask
 * @date 2023/5/4 17:22
 */

@Component
@EnableScheduling
@Slf4j
public class KillTask {

    @Autowired
    TbSecKillMapper tbSecKillMapper;

    @Autowired
    TbSecKillService tbSecKillService;

    @Scheduled(fixedDelay = 1000)
    public void cancelSecKill() {
        log.info("检查秒杀活动是否结束");

        List<TbSecKill> tbSecKills = tbSecKillMapper.selectList(
                new QueryWrapper<TbSecKill>().lambda().eq(TbSecKill::getKillState, 1).le(TbSecKill::getEndTime, new Date())
        );

        tbSecKills.forEach(item -> {
            tbSecKillService.stopSecKIll(item.getKillId());
        });

    }

}
